Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: add optional http timeout #270

Merged
merged 14 commits into from
Jun 8, 2023
Merged

client: add optional http timeout #270

merged 14 commits into from
Jun 8, 2023

Conversation

komuw
Copy link
Owner

@komuw komuw commented Jun 8, 2023

No description provided.

komuw added 12 commits June 7, 2023 22:59
	count := 0
	return func(w http.ResponseWriter, r *http.Request) {
		count = count + 1
		r.Header.Set("MY-COUNT", fmt.Sprint(count))
		wrappedHandler(w, r)
	}
}

func twoMiddleware(wrappedHandler http.Handler) http.HandlerFunc {
	count := 0
	return func(w http.ResponseWriter, r *http.Request) {
		count = count + 1
		r.Header.Set("MY-COUNT", fmt.Sprint(count))
		wrappedHandler.ServeHTTP(w, r)
	}
}

func threeMiddleware(wrappedHandler http.Handler) http.Handler {
	count := 0
	return http.HandlerFunc(
		func(w http.ResponseWriter, r *http.Request) {
			count = count + 1
			r.Header.Set("MY-COUNT", fmt.Sprint(count))
			wrappedHandler.ServeHTTP(w, r)
		},
	)
}

func BenchmarkMid(b *testing.B) {
	b.Run("oneMiddleware", func(b *testing.B) {
		h := oneMiddleware(func(w http.ResponseWriter, r *http.Request) {
			fmt.Fprint(w, "ok")
		})
		rec := httptest.NewRecorder()
		req := httptest.NewRequest(http.MethodGet, "/someUri", nil)

		b.ReportAllocs()
		b.ResetTimer()
		for n := 0; n < b.N; n++ {
			h.ServeHTTP(rec, req)
		}
	})

	b.Run("twoMiddleware", func(b *testing.B) {
		h := twoMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			fmt.Fprint(w, "ok")
		}))
		rec := httptest.NewRecorder()
		req := httptest.NewRequest(http.MethodGet, "/someUri", nil)

		b.ReportAllocs()
		b.ResetTimer()
		for n := 0; n < b.N; n++ {
			h.ServeHTTP(rec, req)
		}
	})

	b.Run("threeMiddleware", func(b *testing.B) {
		h := threeMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			fmt.Fprint(w, "ok")
		}))
		rec := httptest.NewRecorder()
		req := httptest.NewRequest(http.MethodGet, "/someUri", nil)

		b.ReportAllocs()
		b.ResetTimer()
		for n := 0; n < b.N; n++ {
			h.ServeHTTP(rec, req)
		}
	})
}

go test -benchmem -run=^$ -bench ^BenchmarkMid$ github.com/komuw/ong/mux -count=5
BenchmarkMid/oneMiddleware-8      358.7 ns/op
BenchmarkMid/oneMiddleware-8      371.8 ns/op
BenchmarkMid/oneMiddleware-8      343.6 ns/op
BenchmarkMid/oneMiddleware-8      289.0 ns/op
BenchmarkMid/oneMiddleware-8      291.4 ns/op

BenchmarkMid/twoMiddleware-8      296.7 ns/op
BenchmarkMid/twoMiddleware-8      353.3 ns/op
BenchmarkMid/twoMiddleware-8      363.2 ns/op
BenchmarkMid/twoMiddleware-8      366.2 ns/op
BenchmarkMid/twoMiddleware-8      315.2 ns/op

BenchmarkMid/threeMiddleware-8    299.0 ns/op
BenchmarkMid/threeMiddleware-8    307.6 ns/op
BenchmarkMid/threeMiddleware-8    324.9 ns/op
BenchmarkMid/threeMiddleware-8    367.6 ns/op
BenchmarkMid/threeMiddleware-8    373.3 ns/op

The change we want is `threeMiddleware` and it seems competitive with the others.
Base automatically changed from issues/266-handler-everywhere to main June 8, 2023 11:35
@komuw komuw marked this pull request as ready for review June 8, 2023 11:37
@codecov-commenter
Copy link

Codecov Report

Patch coverage: 91.34% and project coverage change: +0.31 🎉

Comparison is base (ce5ce80) 77.58% compared to head (ab870fa) 77.90%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #270      +/-   ##
==========================================
+ Coverage   77.58%   77.90%   +0.31%     
==========================================
  Files          36       36              
  Lines        3337     3362      +25     
==========================================
+ Hits         2589     2619      +30     
+ Misses        623      616       -7     
- Partials      125      127       +2     
Impacted Files Coverage Δ
client/client.go 83.78% <57.14%> (-5.21%) ⬇️
mux/mux.go 76.11% <84.21%> (+4.96%) ⬆️
middleware/auth.go 100.00% <100.00%> (ø)
middleware/client_ip.go 100.00% <100.00%> (ø)
middleware/cors.go 96.44% <100.00%> (ø)
middleware/csrf.go 88.07% <100.00%> (ø)
middleware/fingerprint.go 75.00% <100.00%> (ø)
middleware/gzip.go 68.15% <100.00%> (ø)
middleware/loadshed.go 76.71% <100.00%> (ø)
middleware/log.go 64.38% <100.00%> (ø)
... and 9 more

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@komuw komuw merged commit e940f49 into main Jun 8, 2023
@komuw komuw deleted the client/timeout branch June 8, 2023 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants